package me.firstapp.bbs.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import me.firstapp.bbs.base.BaseController;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.bbs.JsonCollection;
import me.firstapp.common.module.bbs.JsonMember;
import me.firstapp.common.module.bbs.JsonPointAccount;
import me.firstapp.common.module.bbs.JsonPointAccountTrace;
import me.firstapp.common.module.bbs.JsonReply;
import me.firstapp.common.module.bbs.JsonTopic;
import me.firstapp.common.module.shop.JsonAddress;
import me.firstapp.common.module.shop.JsonOrder;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.spi.bbs.CollectionService;
import me.firstapp.spi.bbs.ReplyService;
import me.firstapp.spi.bbs.TopicService;
import me.firstapp.spi.shop.AddressService;
import me.firstapp.spi.shop.OrderService;
import me.firstapp.spi.shop.PointService;

@Controller
@RequestMapping(value = "/memberCenter")
public class MemberCenterController extends BaseController {

	@Value("#{configProperties['qiniu.upload.domain']}")
	private String domain;

	@Autowired
	private TopicService topicService;

	@Autowired
	private AddressService addressService;

	@Autowired
	private PointService pointService;

	@Autowired
	private ReplyService replyService;

	@Autowired
	private CollectionService collectionService;

	@Autowired
	private OrderService orderService;

	@RequestMapping(value = "/profile", method = RequestMethod.GET)
	public ModelAndView memberProfileView(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		modelMap.put("position", 0);
		modelMap.put("domain", domain);
		return new ModelAndView("member/memberPage", modelMap);
	}

	/**
	 * 积分变动记录页面
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/pointRecord", method = RequestMethod.GET)
	public ModelAndView pointRecordView(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		modelMap.put("position", 1);
		Long memberId = getCurrentLoginMemberId(request, response);
		PageObject<JsonPointAccountTrace> result = pointService.findPointAccountTraces(1, 500, memberId);
		modelMap.put("tracePage", result.getPage());

		SingleObject<JsonPointAccount> _result = pointService.findPointAccount(memberId);
		modelMap.put("points", _result.getObject().getCurrentValue());
		return new ModelAndView("member/memberPage", modelMap);
	}

	@RequestMapping(value = "/memberOrders", method = RequestMethod.GET)
	public ModelAndView memberOrdersView(@RequestParam(value = "status", defaultValue = "0") Integer status,
			HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		Long memberId = getCurrentLoginMemberId(request, response);
		modelMap.put("status", status);
		modelMap.put("position", 2);
		PageObject<JsonOrder> orderPageResult = orderService.findOrderPage(1, 500, memberId, status);
		modelMap.put("orderPage", orderPageResult.getPage());
		return new ModelAndView("member/memberPage", modelMap);
	}

	@RequestMapping(value = "/address", method = RequestMethod.GET)
	public ModelAndView myAddressView(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		modelMap.put("position", 3);
		Long memberId = getCurrentLoginMemberId(request, response);
		SingleObject<JsonAddress> result = addressService.findAddressByMemberId(memberId);
		modelMap.put("address", result.getObject());
		return new ModelAndView("member/memberPage", modelMap);
	}

	@RequestMapping(value = "/publish", method = RequestMethod.GET)
	public ModelAndView myPublishView(
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize,
			@RequestParam(value = "tab", required = true, defaultValue = "topic") String tab,
			HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		modelMap.put("position", 4);
		modelMap.put("tab", tab);
		Long memberId = getCurrentLoginMemberId(request, response);
		if ("topic".equals(tab)) {
			PageObject<JsonTopic> topicResult = topicService.findTopicsPage(pageNo, 500, null, memberId, null, null,
					null);
			modelMap.put("topicPage", topicResult.getPage());
		}
		if ("reply".equals(tab)) {
			PageObject<JsonReply> replyResult = replyService.findReplyPage(pageNo, 500, null, memberId);
			modelMap.put("replyPage", replyResult.getPage());
		}
		return new ModelAndView("member/memberPage", modelMap);
	}

	@RequestMapping(value = "/collection", method = RequestMethod.GET)
	public ModelAndView myCollectionView(
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize,
			@RequestParam(value = "tab", required = true, defaultValue = "topic") String tab,
			HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		modelMap.put("position", 5);
		modelMap.put("tab", tab);
		Long memberId = getCurrentLoginMemberId(request, response);
		PageObject<JsonCollection> collectionPage = null;
		if ("topic".equals(tab)) {
			collectionPage = collectionService.findMemberCollections(pageNo, 500, memberId, 0);
		}
		if ("product".equals(tab)) {
			collectionPage = collectionService.findMemberCollections(pageNo, 500, memberId, 1);
		}
		if (collectionPage != null) {
			modelMap.put("collectionPage", collectionPage.getPage());
		}
		return new ModelAndView("member/memberPage", modelMap);
	}

	@RequestMapping(value = "/alterLoginPassword", method = RequestMethod.GET)
	public ModelAndView alterLoginPasswordView(HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		modelMap.put("position", 6);
		return new ModelAndView("member/memberPage", modelMap);
	}

	@RequestMapping(value = "/alterLoginPassword.do", method = RequestMethod.POST)
	public void alterLoginPassword(String oldPassword, String newPassword, String code, HttpServletRequest request,
			HttpServletResponse response) {
		Long memberId = getCurrentLoginMemberId(request, response);
		SingleObject<Object> result = memberService.alterLoginPassword(memberId, oldPassword, newPassword, code);

		String loginToken = getCurrentLoginToken(request, response);
		request.getSession().removeAttribute("login_member_" + loginToken);
		request.getSession().removeAttribute("loginToken");
		request.getSession().removeAttribute("shoppingCart");
		ResponseUtils.renderJson(response, result);
	}

	/**
	 * 设置支付密码页面
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/alterPayPassword", method = RequestMethod.GET)
	public ModelAndView alterPayPasswordView(HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		Long memberId = getCurrentLoginMemberId(request, response);
		SingleObject<JsonPointAccount> result = pointService.findPointAccount(memberId);
		modelMap.put("hasPassword", result.getObject().getHasPassword());
		modelMap.put("position", 7);
		return new ModelAndView("member/memberPage", modelMap);
	}

	@RequestMapping(value = "/alterPayPassword.do", method = RequestMethod.POST)
	public void alterPayPassword(String oldPassword, String newPassword, String code, HttpServletRequest request,
			HttpServletResponse response) {
		Long memberId = getCurrentLoginMemberId(request, response);
		SingleObject<Object> result = pointService.alterPayPassword(memberId, oldPassword, newPassword, code);
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/updateMemberInfo.do", method = RequestMethod.POST)
	public void updateMemberInfo(String avatar, String url, String signature, HttpServletRequest request,
			HttpServletResponse response) {
		Long memberId = getCurrentLoginMemberId(request, response);
		SingleObject<JsonMember> result = memberService.updateMemberInfo(memberId, avatar, url, signature, null);
		ResponseUtils.renderJson(response, result);
	}
}
